home *** CD-ROM | disk | FTP | other *** search
/ Delphi Magazine Collection 2001 / Delphi Magazine Collection 20001 (2001).iso / DISKS / Issue54 / Persist / tiPtnVisitorDB.pas < prev    next >
Encoding:
Pascal/Delphi Source File  |  2000-01-21  |  4.8 KB  |  181 lines

  1. {* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
  2.   (c) TechInsite Pty. Ltd.
  3.   PO Box 429, Abbotsford, Melbourne. 3067 Australia
  4.   Phone: +61 3 9419 6456
  5.   Fax:   +61 3 9419 1682
  6.   Web:   www.techinsite.com.au
  7.   EMail: peter_hinrichsen@techinsite.com.au
  8.  
  9.   Created: Jan 2000
  10.  
  11.   Notes: Family of visitors to manage relational database persistence.
  12.  
  13. * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *}
  14. unit tiPtnVisitorDB;
  15.  
  16. interface
  17. uses
  18.    tiPtnVisitor
  19.   ,tiDBConnection
  20.   ,DBTables
  21.   ;
  22.  
  23. type
  24.  
  25.   // TVisDBAbs: Abstract base database visitor
  26.   //----------------------------------------------------------------------------
  27.   TVisDBAbs = class( TVisitorAbs )
  28.   private
  29.     FQuery        : TQuery ;
  30.     FDBConnection : TtiDBConnection;
  31.     procedure   SetDBConnection(const Value: TtiDBConnection);
  32.   protected
  33.     function    AcceptVisitor : boolean ; override ;
  34.     procedure   Init            ; virtual ;
  35.     procedure   SetupParams     ; virtual ;
  36.     procedure   MapRowsToObject ; virtual ;
  37.     procedure   UpdateObject    ; virtual ;
  38.     property    Query : TQuery read FQuery ;
  39.   public
  40.     constructor Create ; override ;
  41.     destructor  Destroy ; override ;
  42.     property    DBConnection : TtiDBConnection read FDBConnection write SetDBConnection ;
  43.   end ;
  44.  
  45.   // TVisDBSelect: Abstract base visitor for mapping the results of a SELECT
  46.   //               statement to a list of objects.
  47.   //----------------------------------------------------------------------------
  48.   TVisDBSelect = class( TVisDBAbs )
  49.   private
  50.   public
  51.     procedure Execute( pVisited : TVisitedAbs ) ; override ;
  52.   end ;
  53.  
  54.   // TVisDBUpdate: Abstract base visitor for mapping an object to a SQL
  55.   //               CREATE, UPDATE or DELETE statement
  56.   //----------------------------------------------------------------------------
  57.   TVisDBUpdate = class( TVisDBAbs )
  58.   private
  59.   public
  60.     procedure Execute( pVisited : TVisitedAbs ) ; override ;
  61.   end ;
  62.  
  63.  
  64. implementation
  65.  
  66. // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
  67. // *
  68. // * TVisDBAbs
  69. // *
  70. // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
  71. constructor TVisDBAbs.Create;
  72. begin
  73.   inherited Create ;
  74.   FQuery := TQuery.Create( nil ) ;
  75. end;
  76.  
  77. //------------------------------------------------------------------------------
  78. destructor TVisDBAbs.Destroy;
  79. begin
  80.   FQuery.Free ;
  81.   inherited;
  82. end;
  83.  
  84. //------------------------------------------------------------------------------
  85. function TVisDBAbs.AcceptVisitor : boolean;
  86. begin
  87.   // Implement in the concrete.
  88.   result := false ;
  89. end;
  90.  
  91. //------------------------------------------------------------------------------
  92. procedure TVisDBAbs.Init;
  93. begin
  94.   // Do nothing, implement in the concrete
  95. end;
  96.  
  97. //------------------------------------------------------------------------------
  98. procedure TVisDBAbs.SetupParams;
  99. begin
  100.   // Do nothing, implement in the concrete
  101. end;
  102.  
  103. //------------------------------------------------------------------------------
  104. procedure TVisDBAbs.MapRowsToObject;
  105. begin
  106.   // Do nothing, implement in the concrete
  107. end;
  108.  
  109. //------------------------------------------------------------------------------
  110. procedure TVisDBAbs.UpdateObject;
  111. begin
  112.   // Do nothing, implement in the concrete
  113. end;
  114.  
  115. //------------------------------------------------------------------------------
  116. procedure TVisDBAbs.SetDBConnection(const Value: TtiDBConnection);
  117. begin
  118.   FDBConnection := Value;
  119.   if Value <> nil then
  120.     FQuery.DatabaseName := FDBConnection.DatabaseName
  121.   else begin
  122.     if FQuery.Active then
  123.       FQuery.Close ;
  124.     FQuery.DatabaseName := '' ;
  125.   end ;
  126. end;
  127.  
  128. // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
  129. // *
  130. // * TVisDBSelect
  131. // *
  132. // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
  133. procedure TVisDBSelect.Execute(pVisited: TVisitedAbs);
  134. begin
  135.   inherited Execute( pVisited ) ;
  136.   try
  137.     if not AcceptVisitor then
  138.       exit ; //==>
  139.  
  140.     Init            ;
  141.     SetupParams     ;
  142.  
  143.     Query.Open ;
  144.     while not Query.EOF do begin
  145.       MapRowsToObject ;
  146.       Query.Next ;
  147.     end ;
  148.  
  149.     UpdateObject ;
  150.  
  151.   finally
  152.     Visited := nil ;
  153.   end ;
  154. end;
  155.  
  156. // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
  157. // *
  158. // * TVisDBUpdate
  159. // *
  160. // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
  161. procedure TVisDBUpdate.Execute(pVisited: TVisitedAbs);
  162. begin
  163.   inherited Execute( pVisited ) ;
  164.   try
  165.     if not AcceptVisitor then
  166.       exit ; //==>
  167.  
  168.     Init            ;
  169.     SetupParams     ;
  170.     MapRowsToObject ;
  171.     Query.ExecSQL   ;
  172.     UpdateObject    ;
  173.  
  174.   finally
  175.     Visited := nil ;
  176.   end ;
  177.  
  178. end ;
  179.  
  180. end.
  181.